package com.project.study.farming.base.manager;

import com.project.study.farming.base.entity.dto.AuditPageDto;
import com.project.study.farming.base.entity.dto.CropsTypeDto;
import com.project.study.farming.base.entity.dto.SeePageDto;
import com.project.study.farming.base.entity.dto.UploadCropsDto;
import com.project.study.farming.base.entity.po.CropTypePo;
import com.project.study.farming.base.entity.po.CropsPo;
import com.project.study.farming.base.mapper.GuanLiMainMapper;
import com.project.study.farming.common.entity.po.UserPo;
import com.project.study.farming.common.utils.BeanMapper;
import com.project.study.farming.common.utils.CommonUtil;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 管理员manager
 * @since 2020-05-01
 */
@Service
@Getter
public class GuanliManager {

    @Value("${file.upload.path.path}")
    private String filePath;

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private GuanLiMainMapper guanLiMainMapper;

    public List<AuditPageDto> searchAudit(Integer bigType, Integer cropId, Integer auditStatus){
        return this.guanLiMainMapper.searchAudit(bigType, cropId, auditStatus);
    }

    public void deleteRes(String id){
        this.guanLiMainMapper.deleteRes(id, CommonUtil.getUser().getUsername());
    }

    /**
     * 上传
     * @param dto 信息
     */
    public void upload(UploadCropsDto dto){
        MultipartFile file = dto.getImage();
        String path = CommonUtil.upload(filePath, file);
        CropsPo cropsPo = new CropsPo();
        BeanUtils.copyProperties(dto, cropsPo);
        cropsPo.setImage(path);
        cropsPo.setTId(Integer.parseInt(dto.getTId()));
        cropsPo.setCreateBy(CommonUtil.getUser().getUsername());
        cropsPo.setCreateTime(LocalDateTime.now());
        this.beanMapper.insert(cropsPo);
    }

    public List<SeePageDto> seePage(Integer areaId, Integer cropId, Integer plantStatus){
        return this.guanLiMainMapper.seePage(areaId, cropId, plantStatus);
    }

    /**
     * 保存农作物大类
     * @param id 大类id
     * @param image 图片路径
     * @param cropsTypeDto 信息
     */
    public void saveCropType(Integer id, String image, CropsTypeDto cropsTypeDto){
        UserPo userPo = CommonUtil.getUser();
        LocalDateTime now = LocalDateTime.now();
        if(Objects.isNull(id)){
            CropTypePo cropTypePo = new CropTypePo();
            cropTypePo.setIsDelete(103001);
            cropTypePo.setImage(image);
            cropTypePo.setCreateBy(userPo.getUsername());
            cropTypePo.setCreateTime(now);
            cropTypePo.setName(cropsTypeDto.getName());
            this.beanMapper.insert(cropTypePo);
        }else{
            CropTypePo havPo = this.beanMapper.selectOne(CropTypePo.class, String.format("type_id = %s", id));
            havPo.setUpdateBy(userPo.getUsername());
            havPo.setImage(image);
            havPo.setName(cropsTypeDto.getName());
            havPo.setUpdateTime(now);
            this.beanMapper.updateById(havPo);
        }
    }

    /**
     * 保存农作物
     * @param id 大类id
     * @param image 图片路径
     * @param cropsTypeDto 信息
     */
    public void saveCrops(Integer id, String image, CropsTypeDto cropsTypeDto){
        UserPo userPo = CommonUtil.getUser();
        LocalDateTime now = LocalDateTime.now();
        CropsPo po = this.beanMapper.selectOne(CropsPo.class, String.format("id = %s", id));
        po.setImage(image);
        po.setName(cropsTypeDto.getName());
        po.setCreateBy(userPo.getUsername());
        po.setCreateTime(now);
        this.beanMapper.updateById(po);
    }
}
